package generator.controller;

import com.atkt.ktkt.model.vod.Teacher;
import com.atkt.ktkt.vo.vod.TeacherQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import generator.service.TeacherService;
import ktkt.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author KT917
 * @date 2022/9/12
 */

@RestController   //返回json 数据
@RequestMapping("/admin/vod/teacher")
@Api(tags = "讲师管理系统")
//@CrossOrigin //跨域
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    //1.查询所有讲师
//    @ApiOperation("查询所有讲师")
//    @GetMapping("/findAll")
//    public List<Teacher> findAllTeacher(){
//        List<Teacher> list = teacherService.list();
//        return list;
//    }
    @ApiOperation("查询所有讲师")
    @GetMapping("/findAll")
    public Result findAllTeacher() {
        List<Teacher> list = teacherService.list();
        return Result.ok(list);
    }

    //2. 逻辑删除讲师
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@ApiParam(name = "id", value = "ID", required = true) @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    //3.分页查询
    @ApiOperation("条件分页查询")
    @PostMapping("/findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {   //TeacherQueryVo 为查询老师条件
        Page<Teacher> teacherPage = new Page<>(current, limit);
        if (teacherQueryVo == null) {
            List<Teacher> list = teacherService.list();
            Page<Teacher> page = teacherService.page(teacherPage);
            return Result.ok(page);
        } else {
            //获取条件值 进行非空判断  条件封装
            String name = teacherQueryVo.getName();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            Integer level = teacherQueryVo.getLevel();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();

            LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(name)) {
                wrapper.like(Teacher::getName, name);
            }
            if (!StringUtils.isEmpty(level)) {
                wrapper.eq(Teacher::getLevel, level);
            }
            if (!StringUtils.isEmpty(joinDateBegin)) {
                wrapper.ge(Teacher::getJoinDate, joinDateBegin);
            }
            if (!StringUtils.isEmpty(joinDateEnd)) {
                wrapper.le(Teacher::getJoinDate, joinDateEnd);
            }
            IPage<Teacher> page = teacherService.page(teacherPage, wrapper);
            return Result.ok(page);
        }
    }

    @ApiOperation("添加讲师")
    @PostMapping("/saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    //5. 修改 根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable long id){
        Teacher byId = teacherService.getById(id);
        return Result.ok(byId);
    }

    //6.修改   修改最终实现
    @ApiOperation("修改最终实现")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    // 7. 批量删除
    @ApiOperation("批量删除讲师")
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody List<Long> ids){
        boolean b = teacherService.removeByIds(ids);
        if (b) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }



}
